package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootLottery;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.service.FootChipService;
import org.footballmanager.pandafootball.system.service.FootLotteryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/hy/footChip")
public class HyChipController extends BaseController {
	/* 实体名称 */
	private static final String ENTITYNAME = "footChip";
	private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);
    protected static final String FEEDBACK = "nofeedback";

	@Autowired
	private FootChipService footChipService;
	
	@Autowired
	private FootLotteryService footLotteryService;
	
	@RequestMapping(value = "/addChip", method = RequestMethod.POST)
	@ResponseBody
	public String addChip(HttpServletRequest request, @RequestParam Integer type, @RequestParam boolean ispay){
		Date date = new Date();
		FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
		FootLottery lottery = footLotteryService.findLotteryByUserId(user.getId());
		if(null == lottery) {
            return Constants.ERROR;
        }
        return processSwitch(lottery, type, date, user, ispay, request);
	}

	private String processSwitch(FootLottery lottery, int color, Date date, FootUser user, boolean ispay, HttpServletRequest request){
	    switch (color){
            case 1:
                return processSwitchCase1(lottery, date, user, ispay, request);
            case 2:
                return processSwitchCase2(lottery, date, user, ispay, request);
            case 3:
                return processSwitchCase3(lottery, date, user, ispay, request);
            case 4:
                return processSwitchCase4(lottery, date, user, ispay, request);
            case 5:
                return processSwitchCase5(lottery, date, user, ispay, request);
            default:
                return Constants.ERROR;
        }
        }

    private String processSwitchCase5(FootLottery lottery, Date date, FootUser user, boolean ispay, HttpServletRequest request) {
        if(lottery.getLotteryDateOrange().getTime() < date.getTime()){
            return footLotteryService.reComputeOrangeDate(user.getId(), ispay, request);
        }
        if(ispay){
            return footLotteryService.reComputeOrangeDate(user.getId(), true, request);
        }
        return FEEDBACK;
    }

    private String processSwitchCase4(FootLottery lottery, Date date, FootUser user, boolean ispay, HttpServletRequest request) {
        if(lottery.getLotteryDatePurple().getTime() < date.getTime()){
            return footLotteryService.reComputePurpleDate(user.getId(), ispay, request);
        }
        if(ispay){
            return footLotteryService.reComputePurpleDate(user.getId(), true, request);
        }
        return FEEDBACK;
    }

    private String processSwitchCase3(FootLottery lottery, Date date, FootUser user, boolean ispay, HttpServletRequest request) {
        if(lottery.getLotteryDateBlue().getTime() < date.getTime()){
            return footLotteryService.reComputeBlueDate(user.getId(), ispay, request);
        }
        if(ispay){
            return footLotteryService.reComputeBlueDate(user.getId(), true, request);
        }
        return FEEDBACK;
    }

    private String processSwitchCase2(FootLottery lottery, Date date, FootUser user, boolean ispay, HttpServletRequest request) {
        if(lottery.getLotteryDateGreen().getTime() < date.getTime()){
            return footLotteryService.reComputeGreenDate(user.getId(), ispay, request);
        }
        if(ispay){
            return footLotteryService.reComputeGreenDate(user.getId(), true, request);
        }
        return FEEDBACK;
    }

    private String processSwitchCase1(FootLottery lottery, Date date, FootUser user, boolean ispay, HttpServletRequest request) {
        if(lottery.getLotteryDateWhite().getTime() < date.getTime()){
            return footLotteryService.reComputeWhiteDate(user.getId(), ispay, request);
        }
        if(ispay){
            return footLotteryService.reComputeWhiteDate(user.getId(), true, request);
        }
        return FEEDBACK;
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
	public String openViewPage(Model map, HttpServletRequest request) {
		FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
		map.addAttribute("time", new Date().getTime());
		map.addAttribute("footLottery", footLotteryService.findLotteryByUserId(user.getId()));
		map.addAttribute(ENTITYNAME, footChipService.findChipByUserId(user.getId()));
		return VIEWPAGE;
	}	

	@RequestMapping(value = "/compose", method = RequestMethod.GET)
	public String compose(Model map, HttpServletRequest request) {
		FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
		map.addAttribute(ENTITYNAME, footChipService.findChipByUserId(user.getId()));
		return "hy/footChip/Compose";
	}	
	
	@RequestMapping(value = "/compose", method = RequestMethod.POST)
	@ResponseBody
	public String compose(HttpServletRequest request, Integer type) {
		FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
		if(type == null) {
            return Constants.ERROR;
        }
        return processComposeSwitch(type, user, request);
	}

	private String processComposeSwitch(Integer type, FootUser user, HttpServletRequest request){
        switch (type) {
            case 1:
                return footChipService.randomWhiltePerson(user.getId(), request);
            case 2:
                return footChipService.randomGreenPerson(user.getId(), request);
            case 3:
                return footChipService.randomBluePerson(user.getId(), request);
            case 4:
                return footChipService.randomPurplePerson(user.getId(), request);
            case 5:
                return footChipService.randomOrangePerson(user.getId(), request);
            default:
                return Constants.ERROR;
        }
    }
}
